import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    position: 0,
    //hide:false
    top: 0,
    isnav: true,
    shop: [],
    newArr: [],
    price: 0,
  },
  mutations: {
    isHide(state, n) {
      n = Math.abs(parseInt(n));
      state.position = n;
    },
    top(state, n) {
      state.top = n;
    },
    navhide(state, flag) {
      state.isnav = flag;
    },
    add(state, shop) {
      let oldShop = null;
      for (let item of state.shop) {
        if (item.iid === shop.iid) {
          oldShop = item;
        }
      }
      if (oldShop) {
        oldShop.count += 1;
      } else {
        state.shop.push(shop);
      }
    },
    checkOut(state) {
      state.newArr = [];
      for (let item of state.shop) {
        if (item.check) {
          state.newArr.push(item);
        }
      }
    },
    allCheck(state, flag) {
      for (let item of state.shop) {
        item.check = flag;
      }
      state.newArr = [];
      for (let item of state.shop) {
        if (item.check) {
          state.newArr.push(item);
        }
      }
    },
  },
  getters: {
    hide(state) {
      return state.position > 600;
    },
    TabHide(state) {
      return state.position > state.top;
    },
    counter(state) {
      return state.shop.length;
    },
    lenghts(state) {
      return state.shop.length === state.newArr.length;
    },
    newArrLenght(state) {
      return state.newArr.length;
    },
    prices(state) {
      state.price = 0;
      for (let item of state.newArr) {
        state.price += parseInt(item.price);
      }

      return state.price;
    },
  },
  actions: {},
  modules: {},
});
